home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1231 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.2 KB  |  142 lines

  1. Newsgroups: comp.lang.c++
  2. Path: ncrgw2.ncr.com!ncrhub6!daynews!falcon!news
  3. From: Dick Menninger <Dick.Menninger@DaytonOH.ATTGIS.COM>
  4. Subject: Re: Exceptions vs. assertions
  5. X-Nntp-Posting-Host: 149.25.99.44
  6. Message-ID: <DKxrB1.63K@falcon.daytonoh.attgis.com>
  7. Sender: news@falcon.daytonoh.attgis.com (News administrative Login)
  8. Reply-To: Dick.Menninger@DaytonOH.ATTGIS.COM (mennid)
  9. Organization: AT&T Global Information Solutions
  10. X-Newsreader: DiscussIT 2.5.1.3 for MS Windows [AT&T Software Products Division]
  11. References: <4ci0on$77p@news2.ios.com>
  12. Date: Tue, 9 Jan 1996 22:47:24 GMT
  13.  
  14.  
  15. > ==========lalit gidwani, 1/4/96==========
  16. > Dick Menninger (Dick.Menninger@DaytonOH.ATTGIS.COM) wrote:
  17.  
  18. [Stuff deleted] 
  19.  
  20. > Thanks Dick!
  21.  
  22. >      I have a few simple lines of code. Do you think
  23. > the C++ exception mechanism has been properly used here:
  24.  
  25. > class SaveError;     // exception class
  26.  
  27. > void GetDataFromScreen() throw();
  28. > void SaveDataInDatabase() throw( SaveError );
  29. > void SaveCurrentInvoice() throw( SaveError );
  30.  
  31. As long as these functions all catch all exceptions
  32. and convert them into SaveError, this is OK.
  33. Otherwise, an exception from allocation somewhere
  34. will terminate the program without telling the user. 
  35.  
  36. > int main( int argc, char **argv )
  37. > {
  38. >      /*****************
  39. >           Send the selected invoice to screen and
  40. >           wait for user to say he wants to
  41. >           save the current invoice.
  42. >      ******************/
  43. >      try
  44. >      {
  45. >           SaveCurrentIncoice();
  46.  
  47. Do you want to tell user success here?
  48.  
  49. >      }
  50. >      catch( SaveError se )
  51. >      {
  52. >           // send error message to the user
  53. >      }
  54. > }// main
  55.  
  56. From the limited specification provided, it is not
  57. clear at all whether this is the best expression
  58. of the problem.  It may make more sense for
  59. SaveCurrentInvoice() to return a boolean of
  60. success==true and failure==false.  Then
  61. the use of an exception, if any, would be
  62. internal to the saving algorithm, assuming
  63. that is the best expression of what needs
  64. to be done there.  Even that cannot be determined
  65. from your limited spec.
  66.  
  67.  
  68. > void SaveCurrentInvoice() throw ( SaveError )
  69. > {
  70. >      GetDataFromScreen() // always succeeds in this system
  71. >      try 
  72. >      {
  73. >           SaveDataToDatabase();
  74. >      }
  75. >      catch( SaveError err )
  76. >      {
  77.  
  78. was anything else expected to be done here?
  79.  
  80. >           throw err;
  81. >      }
  82. > }
  83.  
  84. This is most likely code bloat.  What purpose
  85. were you trying to serve by catching and rethrowing?
  86.  
  87.  
  88. > If this code seems OK, I have two comments:
  89.  
  90. > 1. It seems to increase the amount of code. However, it seems
  91. >    that the exception-handling would reduce code if there
  92. >    where many lines of code in a try block that could throw
  93. >    exceptions.
  94. > 2. This seems to be an issue that needs to be thought out well.
  95.  
  96. > P.S. The syntax may not be absolutely correct.
  97.  
  98.  
  99. > Thanx
  100. > LG
  101.  
  102. The key point is that looking for a "best expression"
  103. of a problem is an ideal and requires more information
  104. than taking any old valid expression of the problem.
  105. In general, we never know if the expression is best,
  106. but we often know that we have found a much better
  107. expression than our earlier attempts when we try.
  108. This leads to our growing an experience base of
  109. patterns of solutions that we recognize more readily
  110. and raise the quality of this form of endeavor as we
  111. attack new problems.
  112.  
  113. There is only a rather limited experience base with
  114. possible patterns involving exceptions.  Everyone,
  115. including the language definers are still groping with
  116. their basic implications.  As part of the "future shock"
  117. being caused adding them to the language, people
  118. are taking all kinds of very restricted views towards
  119. them and are applying them badly and then saying
  120. it is the fault of exceptions.  I am trying to add what
  121. seems to be needed tension in the other direction
  122. by making what seem to me to be basic obvious
  123. statements.
  124.  
  125. One such is stating that this is a control paradigm.
  126. Some want to say that it only an error paradigm.
  127. Well that is mixing what it is with how you want
  128. to [artifically, at least to me] limit use of it.  This
  129. difference has hurt how well it was concieved
  130. when it was added to the language.
  131.  
  132.  
  133. Good Day
  134. Dick
  135. Dick.Menninger@DaytonOH.ATTGIS.COM
  136.  
  137.